<?php
/**
 * Created by PhpStorm.
 * User: yuyiwen
 * Date: 2018/9/22
 * Time: 上午10:45
 */

namespace app\admin\controller;


use think\Image;
use think\Request;
use think\Session;

class Ad extends Common
{
    public function index ()
    {

        $search = input('get.search' , '' , 'htmlspecialchars');
        $page_search = $search;
        $ad = db('ad');
        if ($search){
            ini_set('memory_limit', '1024M');
            require EXTEND_PATH.'jieba/vendor/multi-array/MultiArray.php';
            require EXTEND_PATH.'jieba/vendor/multi-array/Factory/MultiArrayFactory.php';
            require  EXTEND_PATH.'jieba/class/jieba.php';
            require  EXTEND_PATH.'jieba/class/Finalseg.php';

            \Fukuball\Jieba\Jieba::init();
            \Fukuball\Jieba\ Finalseg::init();
            $search = \Fukuball\Jieba\Jieba::cut($search);

            for ($i = 0; $i<count($search) ; $i++){
                $search[$i] = '%'.$search[$i].'%';
            }

            $where = '';
            for ($i = 0; $i<count($search) ; $i++){
                $where .= ' `url` LIKE "'.$search[$i]. '" OR ';
            }

            $where = rtrim($where , 'OR ');
            $ad->where($where);


        }
//        $res =  $ad->buildSql();
//        dump($res);
//        die;
        $ad_list = $ad->paginate(2 , false , ['query'=>['search'=>$page_search]]);

        $current_page = input('get.page' , 1 , 'intval');

        $page = $ad_list->render();
        $page = str_replace(
            ['class="pagination"' , 'class="disabled"' , 'class="active"'],
            ['class="am-pagination tpl-pagination"' , 'class="am-disabled"' , 'class="am-active"'],
            $page
        );
        $request = Request::instance();
        if ( $request->isAjax() )
        {
            return json(['ad_list' => $ad_list  , 'page' => $page , 'current_page'=>$current_page]);
        }
        $this->assign('page' , $page);
        $this->assign('current_page' , $current_page);
        $this->assign('ad_list' , $ad_list);
        return view();
    }

    /**
     * 新增轮播图
     */
    public function create ()
    {
        return view();
    }

    /**
     * 保存轮播图
     */
    public function store ()
    {
        // 文件上传
        $file = request()->file('src');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $post['src']  = $info->getSaveName();
                # 制作缩略图
                $image  = Image::open(ROOT_PATH . 'public' . DS . 'uploads/' . $post['src']);
//                print_r($image);die;
                if (!file_exists(dirname(ROOT_PATH . 'public' . DS .'/uploads/thumb/'. $post['src']))){
                    mkdir(dirname(ROOT_PATH . 'public' . DS .'/uploads/thumb/'. $post['src']) , 0777, true);
                }
                $image->thumb('200' , '100' , Image::THUMB_FILLED) -> save( ROOT_PATH . 'public' . DS .'/uploads/thumb/'. $post['src'] , null , 20);
            }else{
                $this->error( $file->getError() );
            }
        }
        $post['ord'] = input('post.ord' , '' , 'htmlspecialchars');
        $post['url'] = input('post.url' , '' , 'htmlspecialchars');
        $post['is_show'] = input('post.is_show' , '1' , 'htmlspecialchars');

        $id = db('ad')->insertGetId($post);
        if($id){
            $this->success('新增轮播图成功' , url('index'));
        }

    }


    /**
     * 删除轮播图
     */
    public function destroy ()
    {
        $id = input('get.id');
        $info = db('ad')->find($id);
        if (file_exists(ROOT_PATH . 'public' . DS . 'uploads/'.$info['src']))
        unlink(ROOT_PATH . 'public' . DS . 'uploads/'.$info['src']);
        if (file_exists(ROOT_PATH . 'public' . DS . 'uploads/thumb/'.$info['src']))
        unlink(ROOT_PATH . 'public' . DS . 'uploads/thumb/'.$info['src']);
        $id = db('ad')->delete($id);
        if ($id) {
            $this->success('删除成功' , url('index'));
        }else {
            $this->error('删除失败');
        }
    }
}
